//
//  OEMineViewController.m
//  OneEightyNine
//
//  Created by apple on 2019/3/3.
//  Copyright © 2019 stoneobs. All rights reserved.
//

#import "OEMineViewController.h"
#import "OEUserInfoViewController.h"
#import "OEPakageHomeViewController.h"
#import "OEQrCodeViewController.h"
#import "OECollectionViewController.h"
#import "OEPresuppositionViewController.h"
#import "OEShareViewController.h"
#import "OEMemberCenterViewController.h"
#import "ONSetUpHomeViewController.h"
#import <LocalAuthentication/LocalAuthentication.h>

@interface OEMineViewController ()
@property(nonatomic, strong) NSArray                     *dataSouce;/**<  */
@end

@implementation OEMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configSubView];
    [self.view bringSubviewToFront:self.tableView];
    //获取相册权限
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
        
    }];
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [OEUserDataController sendFetchUserInfoRequest:^(BOOL success, NSString *errorString, NSDictionary *resp) {
        if (success) {
            OEUserModel * user = [OEUserModel mj_objectWithKeyValues:resp];
            [STUserManger.defult updateUserModel:user];
            [self configTableHeader];
        }
    }];
}
#pragma mark --configSubView
- (void)configSubView{
    [self vhl_setNavBarHidden:YES];
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 160)];
    imageView.image = [UIImage imageNamed:@"750×320背景"];
    [self.view insertSubview:imageView atIndex:0];
    self.tableView.backgroundColor = UIColor.clearColor;
    
    self.tableView.contentInset = UIEdgeInsetsZero;
    self.tableView.frame = CGRectMake(0, 0, UIScreenWidth, TMUtils.tabBarTop );
    
    [self configTableHeader];
    [self configDataSouce];
    
    STLabel * titleLabel = [[STLabel alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 44)
                                                     text:OESTRING(@"CUSTOMER PHONE:400-0000-000")
                                               textColor:FirstTextColor
                                                    font:15
                                             isSizetoFit:NO
                                           textAlignment:NSTextAlignmentCenter];
    titleLabel.backgroundColor = OE_GrayColor;
    self.tableView.tableFooterView = titleLabel;
}
- (void)configTableHeader{
    __weak typeof(self) weakSelf =  self;
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    
    UIView * whiteView = [[UIView alloc] initWithFrame:CGRectMake(20, TMUtils.navgationBarBootom + 10, UIScreenWidth - 40, 110)];
    whiteView.backgroundColor = UIColor.whiteColor;
    whiteView.clipsToBounds = YES;
    whiteView.layer.cornerRadius = 8;
    whiteView.clipsToBounds = YES;
    [header addSubview:whiteView];
    [whiteView st_showRoundLayer];
    whiteView.userInteractionEnabled = YES;
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotoUserInfoController)];
    [whiteView addGestureRecognizer:tap];
    
    header.height = whiteView.bottom  + 10;
    self.tableView.tableHeaderView = header;
    OEUserModel * user = STUserManger.defult.loginedUser;
    ({
        STButton * button = [[STButton alloc] initWithFrame:CGRectMake(20, 20, 70, 70)
                                                      title:nil
                                                 titleColor:nil
                                                  titleFont:0
                                               cornerRadius:35
                                            backgroundColor:OE_GrayColor
                                            backgroundImage:nil
                                                      image:nil];
        [button sd_setBackgroundImageWithURL:[NSURL URLWithString:user.thumb] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"80×80默认头像"]];
        [whiteView addSubview:button];
        [button setClicAction:^(UIButton *sender) {
            [weakSelf gotoUserInfoController];
        }];
        STLabel * titleLabel = [[STLabel alloc] initWithFrame:CGRectMake(button.right  +  10, button.top - 5, 200, 40)
                                                         text:user.user_name
                                                    textColor:UIColor.blackColor
                                                         font:16
                                                  isSizetoFit:NO
                                                textAlignment:NSTextAlignmentLeft];
        [whiteView addSubview:titleLabel];
        
        STButton * contentbutton = [[STButton alloc] initWithFrame:CGRectMake(button.right + 10, 0, 140, 40)
                                                      title:OESTRING(@"SINGLE USE  PAASTIC \n CUP FREE SINCE 2018 ")
                                                 titleColor:UIColor.whiteColor
                                                  titleFont:10
                                               cornerRadius:20
                                            backgroundColor:OE_LIMITE_COLOR
                                            backgroundImage:nil
                                                      image:nil];
        contentbutton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        contentbutton.bottom = button.bottom;
        contentbutton.titleLabel.numberOfLines = 0;
        contentbutton.titleLabel.textAlignment = NSTextAlignmentCenter;
        [whiteView addSubview:contentbutton];
        
        
        
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 7, 12)];
        imageView.image = [UIImage imageNamed:@"14×24头像箭头"];
        [whiteView addSubview:imageView];
        
        imageView.right = whiteView.width - 15;
        imageView.centerY = whiteView.height / 2;
        
    });
    [self.tableView reloadData];
}
- (void)configDataSouce{
    
    self.dataSouce = @[
                       [[STTableEasyModel alloc] initWithTextString:@"WALLET" detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:@"QR CODE" detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:@"MY COLLECTION" detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:@"PRESUPPOSITION" detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:@"INVITING  FRIENDS" detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:@"MERCHANT ENTRY" detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:@"SET UP" detailString:@""]
                       ];
    [self.tableView reloadData];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    STTableEasyModel * model = self.dataSouce[indexPath.row];
    [cell setSt_tableEasyModel:model];
    return cell;
}

#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    STTableEasyModel * model = self.dataSouce[indexPath.row];
    if ([model.textString containsString:@"WALLET"]) {
        [self.navigationController pushViewController:OEPakageHomeViewController.new animated:YES];
    }
    if ([model.textString containsString:@"QR CODE"]) {
        [self.navigationController pushViewController:OEQrCodeViewController.new animated:YES];
        
    }
    if ([model.textString containsString:@"MY COLLECTION"]) {
        [self.navigationController pushViewController:OECollectionViewController.new animated:YES];
        
    }
    if ([model.textString containsString:@"PRESUPPOSITION"]) {
        [self.navigationController pushViewController:OEPresuppositionViewController.new animated:YES];
    }
    if ([model.textString containsString:@"INVITING  FRIENDS"]) {
        [self.navigationController pushViewController:OEShareViewController.new animated:YES];
        
    }
    if ([model.textString containsString:@"MERCHANT ENTRY"]) {
        [self.navigationController pushViewController:OEMemberCenterViewController.new animated:YES];
        
    }
    if ([model.textString containsString:@"SET UP"]) {
        [self.navigationController pushViewController:ONSetUpHomeViewController.new animated:YES];
        
    }
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)gotoUserInfoController{
      [self.navigationController pushViewController:OEUserInfoViewController.new animated:YES];
}


@end
